var app = getApp();
Page({
  data: {
    name: "",
    mobile: "",
    detail: "",
    region: [],
    editAddress: ''
  },
  onLoad: function(e) {
    console.log(e)
    if (e == null) {} else {
      this.setData({
        editAddress: e.editAddress
      })
      var that = this
      e.editAddress && app.getData({
        url: app.api.selectUserByAdd(),
        method: 'GET',
        data: {
          id: e.editAddress
        },
        ok: function(e) {
          console.log(e),
            that.setData({
              name: e.data.data.hyName,
              mobile: e.data.data.telNumber,
              detail: e.data.data.detailInfo,
              region: e.data.data.provinceName
            });
        }
      });
    }

  },
  bindRegionChange: function(e) {
    console.log("picker发送选择改变，携带值为", e.detail.value), this.setData({
      region: e.detail.value
    });
  },
  dingwei: function(e) {
    console.log(e);
    var t = this;
    wx.chooseLocation({
      success: function(e) {
        console.log(e);
        var a = e.address.indexOf("区");
        console.log(e.address.substring(a + 1) + e.name), t.setData({
          detail: e.address.substring(a + 1) + e.name
        });
      },
      fail: function() {
        wx.showModal({
          title: "提示",
          content: "您点击了拒绝位置授权，部分功能无法使用,点击确定重新获取授权。",
          showCancel: !1,
          success: function(e) {
            e.confirm && wx.openSetting({
              success: function(e) {
                e.authSetting["scope.userLocation"] && t.dingwei();
              },
              fail: function(e) {}
            });
          }
        });
      }
    });
  },
  formSubmit: function(e) {
    console.log("form发生了submit事件，携带数据为：", e.detail.value);

    var a = wx.getStorageSync("bizUserInfo").id,
      t = e.detail.value.name,
      o = e.detail.value.mobile,
      n = e.detail.value.picker.toString(),
      i = e.detail.value.detail,
      s = this.data.editAddress;
    console.log(a, t, o, n, i, s);
    console.log(s)
    var l = "",
      c = !0;
    "" == t ? l = "请填写收货人！" : "" == o ? l = "请填写手机号！" : /^0?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$/.test(o) && 11 == o.length ? "" == n ? l = "请选择所在地区！" : "" == i ? l = "请填写详细地址！" : (c = !1,
      wx.showLoading({
        title: "保存中...",
        mask: !0
      }),
      s == null ? app.getData({
        url: app.api.seveUserAddress(),
        method: 'POST',
        data: {
          detailInfo: i,
          provinceName: n,
          hyName: t,
          hyId: a,
          telNumber: o,
          isSend: 1,
          isDefault: 0
        },
        ok: function(res) {
          console.log(res),
            "1" == res.data.code && (wx.showToast({
              title: '保存成功',
              duration: 1e3
            }))
          setTimeout(function() {
            wx.navigateBack({
              delta: 1
            });
          }, 1e3)
        }
      }) : app.getData({
        url: app.api.updataUserAdd(),
        method: 'POST',
        data: {
          detailInfo: i,
          provinceName: n,
          hyName: t,
          id: s,
          telNumber: o,
          isSend: 1,
          isDefault: 0
        },
        ok: function(res) {
          console.log(res),
            "1" == res.data.code && (wx.showToast({
              title: '修改成功',
              duration: 1e3
            }))
          setTimeout(function() {
            wx.navigateBack({
              delta: 1
            });
          }, 1e3)
        }
      })) : l = "手机号错误！", 1 == c && wx.showModal({
      title: "提示",
      content: l
    });
  },
  onReady: function() {},
  onShow: function() {}
});